package cn.yuanqiao.cqbc.deleteIdentify.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/21 14:22
 */

import cn.yuanqiao.activiti.domain.ActivitiPackage;
import cn.yuanqiao.activiti.service.RemoteActivitiService;
import cn.yuanqiao.feignService.RemoteCommonService;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.utils.PageUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.common.mapper.MapperToolsMapper;
import cn.yuanqiao.cqbc.deleteIdentify.mapper.DeleteIdentifyMapper;
import cn.yuanqiao.cqbc.deleteIdentify.service.IDeleteIdentifyService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/21 14:22
 *@Description:
 *@Note:
 */
@Service
public class DeleteIdentifyServiceImpl implements IDeleteIdentifyService {

    @Autowired
    private DeleteIdentifyMapper deleteIdentifyMapper;
    @Autowired
    private RemoteCommonService remoteCommonService;
    @Autowired
    private MapperToolsMapper DbTool;
    @Autowired
    private RemoteActivitiService activitiService;

    @Override
    public List<TreeSelect> getTreeList() {
        R<List<TreeSelect>> resultR=remoteCommonService.getdanganTree();
        return resultR.getData();
    }


    @Override
    public List<Map<String, Object>> getList(JSONObject json) {
        Long categoryId = json.getLong("categoryId");//门类id
        String orderstr = json.getString("orderstr");//门类id
        if(null!=orderstr&&!"".equals(orderstr)){
            orderstr=" order by "+orderstr;
        }
        /**查询列表并返回*/
        return deleteIdentifyMapper.getList(categoryId,orderstr);
    }

    @Override
    public int fqsp(JSONObject json) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        Long id=json.getLong("id");
        Long categoryId=json.getLong("categoryId");
        Map map=new HashMap(1<<1);
        map.put("id",id);
        map.put("categoryId",categoryId);
        Map<String, Object> variables = new HashMap();
        variables.put("processVariables", map);
        R<ActivitiPackage> activitiPackageR = activitiService.startProcessWorkFlowCQBCDelete(variables);
        String activitiId = activitiPackageR.getData().getProcessInstance().getId();
        //更新表数据的delete_state 0:审批中 1:审批结束
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set delete_state=0 where id ="+id+";update "+tenantCode+"_CQBC_HDDA set delete_state=0 where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set delete_state=0 where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set delete_state=0 where data_id="+id+";");

        return 1;
    }

    @Override
    public int cqbcDeleteBanjie(Map<String,Object> map) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String id=String.valueOf(map.get("id"));
        String categoryId=String.valueOf(map.get("categoryId"));
        //更新表数据的delete_state 0:审批中 1:审批结束
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set delete_state=1,is_del=1 where id ="+id+";update "+tenantCode+"_CQBC_HDDA set delete_state=1,is_del=1 where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set delete_state=1,is_del=1 where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set delete_state=1,is_del=1 where data_id="+id+";");
        return 1;
    }

    @Override
    public void killcqbcDelete(Map<String,Object> map) {
        String tenantCode= SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        String id=String.valueOf(map.get("id"));
        String categoryId=String.valueOf(map.get("categoryId"));
        //更新表数据的delete_state 0:审批中 1:审批结束
        DbTool.executeUpdate("UPDATE YQ_DATA_MANAGE set delete_state = NULL where id ="+id+";update "+tenantCode+"_CQBC_HDDA set delete_state = NULL where data_id="+id+";update "+tenantCode+"_CQBC_FOLDER set delete_state = NULL where data_id="+id+";update "+tenantCode+"_CQBC_ITEM set delete_state = NULL where data_id="+id+";");
    }


}
